package com.gengine.keyBoard
{
	import com.gengine.global.*;
	import fl.controls.listClasses.*;
	import flash.display.*;
	import flash.events.*;
	import flash.system.*;
	import flash.text.*;
	
	public class KeyBoardManager extends EventDispatcher
	{
		private static var _keyMap:Object = {};
		private static var _instance:KeyBoardManager;
		private static var _stage:Stage;
		public static var ctrlKey:Boolean;
		
		public function KeyBoardManager()
		{
			if (_instance != null)
			{
				throw new Error("KeyBoardManager 不能实例化");
			}
			_instance = this;
			
		}
		
		public function start() : void
		{
			_stage = Global.stage;
			if (_stage)
			{
				this.addListener();
			}
			
		}
		
		public function cancelListener() : void
		{
			if (_stage)
			{
				_stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDownHandler);
				_stage.removeEventListener(KeyboardEvent.KEY_UP, this.onKeyUpHandler);
				_stage.removeEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, this.onMouseFocusChangeHandler);
				_stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDownHandler);
			}
			
		}
		
		public function addListener() : void
		{
			if (_stage)
			{
				_stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDownHandler, false, 9999, false);
				_stage.addEventListener(KeyboardEvent.KEY_UP, this.onKeyUpHandler, false, 9999, false);
				_stage.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, this.onMouseFocusChangeHandler, false, 9999, false);
				_stage.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDownHandler, false, 9999, false);
			}
			
		}
		
		private function onMouseFocusChangeHandler(event:FocusEvent) : void
		{
			if (event.relatedObject is TextField)
			{
				if ((event.relatedObject as TextField).type == TextFieldType.INPUT)
				{
					this.changeImeEnable(true);
				}
			}
			else if (event.relatedObject is CellRenderer)
			{
			}
			else
			{
				_stage.focus = _stage;
				this.changeImeEnable(false);
			}
			
		}
		
		public function changeImeEnable(param1:Boolean) : void
		{
			var enbled:* = param1;
			if (enbled == IME.enabled)
			{
				
			}
			if (Capabilities.hasIME)
			{
				try
				{
					IME.enabled = enbled;
				}
				catch (e:Error)
				{
				}
			}
			
		}
		
		private function onMouseDownHandler(event:MouseEvent) : void
		{
			KeyBoardManager.ctrlKey = event.ctrlKey;
			
		}
		
		private function onKeyDownHandler(event:KeyboardEvent) : void
		{
			var _loc_4:KeyEvent = null;
			var _loc_2:* = event.keyCode;
			var _loc_3:* = getKeyData(_loc_2);
			if (_loc_3.isKeyDown == false)
			{
				_loc_3.isKeyDown = true;
				_loc_4 = new KeyEvent(KeyEvent.KEY_DOWN);
				_loc_4.keyEvent = event;
				dispatchEvent(_loc_4);
			}
			event.stopImmediatePropagation();
			
		}
		
		private function onKeyUpHandler(event:KeyboardEvent) : void
		{
			var _loc_4:KeyEvent = null;
			var _loc_2:* = event.keyCode;
			var _loc_3:* = getKeyData(_loc_2);
			if (_loc_3.isKeyDown == true)
			{
				_loc_3.isKeyDown = false;
				_loc_4 = new KeyEvent(KeyEvent.KEY_UP);
				_loc_4.keyEvent = event;
				dispatchEvent(_loc_4);
			}
			event.stopImmediatePropagation();
			
		}
		
		public static function get instance() : KeyBoardManager
		{
			if (_instance == null)
			{
				_instance = new KeyBoardManager;
			}
			return _instance;
		}
		
		public static function addkeys(param1:Array) : void
		{
			var _loc_3:KeyData = null;
			if (param1 == null || param1.length == 0)
			{
				
			}
			if (param1[0] as KeyData == null)
			{
				throw new Error("addkeys(value:Array) value中不是KeyData类型");
			}
			var _loc_2:* = param1.length;
			var _loc_4:int = 0;
			while (_loc_4 < _loc_2)
			{
				
				_loc_3 = param1[_loc_4] as KeyData;
				_keyMap[_loc_3.keyCode] = _loc_3;
				_loc_4++;
			}
			
		}
		
		public static function getKeyData(param1:uint) : KeyData
		{
			var _loc_2:* = _keyMap[param1] as KeyData;
			if (_loc_2 == null)
			{
				_loc_2 = new KeyData(param1);
				_loc_2.isKeyDown = false;
				_keyMap[param1] = _loc_2;
			}
			return _loc_2;
		}
		
	}
}
